/**----------------------------------------
 * @description 音乐音效文件定义
 * @author      lxc
 * @file        AudioManager.ts
 * @Date        2022-05-05
 * @version     1.0.0
 * ----------------------------------------
 */

import { LogClass } from "../LogClass";
import { EffectFile, MusicFile } from "./AudioFile";

export class AudioManager extends LogClass{
    /**
     * 播放状态
     */
    private playState = true;
    /**
     * 背景音量
     */
    private musicValue = 0.5;
    /**
     * 特效音量
     */
    private effectValue = 0.5

    constructor(){
        super()
        if(CC_DEBUG){
            if(CC_JSB){

            }else{
                this.playState = false;  // 在 pc 上不播放音乐音效
            }
        }
    }
    /**
     * 播放音效
     * @param effect 
     * @param volume 
     */
    playEffect(effect : EffectFile, volume : number = -1){
        if(this.playState){
            let audio = 0 ;
            cc.loader.loadRes(effect, cc.AudioClip,(error,clip)=>{
                if(error){
                    this.printError("playEffect error :", effect)
                    return;
                }
                volume = volume > 0 ? volume : this.effectValue;
                audio = cc.audioEngine.play(clip,false,volume);
            })
            return audio;
        }
    }
    /**
     * 播放背景音乐
     * @param music 
     * @param volume 
     */
    playMusic(music : MusicFile, volume : number = -1){
        if(this.playState){
            let audio = 0;
            cc.loader.loadRes(music, cc.AudioClip,(error,clip)=>{
                if(error){
                    this.printError("playeMusic error :", music)
                    return;
                }
                volume = volume > 0 ? volume : this.effectValue;
                audio = cc.audioEngine.playMusic(clip,false);
                cc.audioEngine.setMusicVolume(volume);
            })
            return audio;
        }
    }

    pauseEffect(audioId: number): void{
        cc.audioEngine.pause(audioId);
    }

    resumeEffect(audioId: number):void{
        cc.audioEngine.resume(audioId);
    }

    stopEffect(audioId: number):void{
        cc.audioEngine.stop(audioId);
    }

    pauseAll():void{
        cc.audioEngine.pauseAll();
    }

    resumeAll(): void{
        cc.audioEngine.resumeAll();
    }

    stopAll():void{
        cc.audioEngine.stopAll();
    }

    stopMusic(): void{
        cc.audioEngine.stopMusic();
    }
    
    pauseMusic(): void{
        cc.audioEngine.pauseMusic();
    }

    resumeMusic(): void{
        cc.audioEngine.resumeMusic();
    }

    setMusicVolume(prog): void{
        cc.audioEngine.setMusicVolume(prog);
        this.musicValue = prog;
    }

    setEffectVolume(prog): void{
        cc.audioEngine.setEffectsVolume(prog);
        this.effectValue = prog;
    }
}